package com.hksj.common.core.model;

import cn.hutool.http.HttpStatus;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 统一响应体
 * @param <T> data指代的类型
 */
@Data
public class Rest<T> {
    private int code;
    private String msg;
    private T data;
    private String requestId;
    private String requestTime;

    public Rest(){
    }

    public Rest(Integer code, String msg, T o) {
        this.code = code;
        this.msg = msg;
        this.data = o;
    }
 
    public static Rest ok() {
        return ok("操作成功",null);
    }

    public static <T> Rest <T> ok(T data) {
        return ok("操作成功",data);
    }

    public static <T> Rest <T> ok(String msg,T data) {
        Rest<T> rest = new Rest<>(HttpStatus.HTTP_OK, msg, data);
        return setAddition(rest);
    }

    public static <T> Rest <T> fail(String msg) {
        return fail(HttpStatus.HTTP_INTERNAL_ERROR, msg);
    }

    public static <T> Rest <T> fail(int code, String msg) {
        Rest<T> rest = new Rest<>(code, msg, null);
        return setAddition(rest);
    }

    private static <T> Rest <T> setAddition(Rest rest){
        rest.setRequestId(UUID.randomUUID().toString().replace("-", ""));
        rest.setRequestTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return rest;
    }
}